home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b12 / Util / MemoryUtil.c < prev    next >
Text File  |  1995-12-09  |  3KB  |  219 lines

  1. /*****
  2.  *
  3.  *    MemoryUtil.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp1.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #include "compiler_stuff.h"
  18. #include "globals.h"
  19.  
  20. #include "MemoryUtil.h"
  21.  
  22.  
  23. /***  MEMORY ALLOCATION  ***/
  24.  
  25. /*    I use these 'wrappers' to toolbox memory calls to provide a way
  26.     to consistantly handle error conditions. */
  27.  
  28. Handle
  29. MyNewHandle ( long theSize, OSErr *outErr )
  30. {
  31.     Handle    theHandle;
  32.     OSErr    theErr;
  33.     
  34.     theHandle    = NewHandle    ( theSize );
  35.     theErr        = MemError    ();
  36.     
  37.     if ( outErr != nil )
  38.     {
  39.         *outErr = theErr;
  40.     }
  41.     
  42.     if ( theErr != noErr )
  43.     {
  44.         return nil;
  45.     }
  46.     else
  47.     {
  48.         return theHandle;
  49.     }
  50. }
  51.  
  52.  
  53. Handle
  54. MyNewHandleClear ( long theSize, OSErr *outErr )
  55. {
  56.     Handle    theHandle;
  57.     OSErr    theErr;
  58.     
  59.     theHandle    = NewHandleClear    ( theSize );
  60.     theErr        = MemError            ();
  61.     
  62.     if ( outErr != nil )
  63.     {
  64.         *outErr = theErr;
  65.     }
  66.     
  67.     if ( theErr != noErr )
  68.     {
  69.         return nil;
  70.     }
  71.     else
  72.     {
  73.         return theHandle;
  74.     }
  75. }
  76.  
  77.  
  78. Ptr
  79. MyNewPtr ( long theSize, OSErr *outErr )
  80. {
  81.     Ptr        thePtr;
  82.     OSErr    theErr;
  83.     
  84.     thePtr    = NewPtr    ( theSize );
  85.     theErr    = MemError    ();
  86.     
  87.     if ( outErr != nil )
  88.     {
  89.         *outErr = theErr;
  90.     }
  91.     
  92.     if ( theErr != noErr )
  93.     {
  94.         return nil;
  95.     }
  96.     else
  97.     {
  98.         return thePtr;
  99.     }
  100. }
  101.  
  102.  
  103. Ptr
  104. MyNewPtrClear ( long theSize, OSErr *outErr )
  105. {
  106.     Ptr        thePtr;
  107.     OSErr    theErr;
  108.     
  109.     thePtr    = NewPtrClear    ( theSize );
  110.     theErr    = MemError        ();
  111.     
  112.     if ( outErr != nil )
  113.     {
  114.         *outErr = theErr;
  115.     }
  116.     
  117.     if ( theErr != noErr )
  118.     {
  119.         return nil;
  120.     }
  121.     else
  122.     {
  123.         return thePtr;
  124.     }
  125. }
  126.  
  127.  
  128. /***  EMERGENCY MEMORY  ***/
  129. #pragma mark -
  130.  
  131. /* IM-Memory: 1-46 */
  132. Boolean
  133. InitializeEmergencyMemory ( OSErr *outErr )
  134. {
  135.     Boolean     success;
  136.     OSErr        theErr;
  137.     
  138.     success = true;
  139.     
  140.     gEmergencyMemory = NewHandle    ( kMemCushionSize );
  141.     theErr             = MemError        ();
  142.     
  143.     if ( outErr != nil )
  144.     {
  145.         *outErr = theErr;
  146.     }
  147.     
  148.     if ( theErr != noErr )
  149.     {
  150.         gEmergencyMemory = nil;
  151.         success             = false;
  152.     }
  153.     else
  154.     {
  155.         SetGrowZone ( NewGrowZoneProc(MyGrowZone) );
  156.     }
  157.     
  158.     return success;
  159. } /* InitializeEmergencyMemory */
  160.  
  161.  
  162. /* IM-Memory: 1-48 */
  163. pascal long
  164. MyGrowZone ( Size cbNeeded )
  165. {
  166.     OSErr    theErr;
  167.     long    theA5;
  168.     long    theSize;
  169.     
  170.     theA5 = SetCurrentA5 ();
  171.     
  172.     if ( *gEmergencyMemory && ( gEmergencyMemory != GZSaveHnd() ) )
  173.     {
  174.         EmptyHandle ( gEmergencyMemory );
  175.         
  176.         theErr = MemError ();
  177.         
  178.         if ( theErr != noErr )
  179.         {
  180.             theSize = nil;
  181.         }
  182.         else
  183.         {
  184.             theSize = kMemCushionSize; /* kEmergencyMemorySize in IM */
  185.         }
  186.     }
  187.     else
  188.     {
  189.         theSize = nil;
  190.     }
  191.     
  192.     theA5 = SetA5 ( theA5 );
  193.     
  194.     return theSize;
  195. } /* MyGrowZone */
  196.  
  197.  
  198. /* To check that the memory reserve is intact.
  199.     Returns true if there is emergency memory on reserve.
  200.     IM-Memory: 1-47 */
  201. Boolean
  202. IsEmergencyMemAvail ( void )
  203. {
  204.     return ( (gEmergencyMemory != nil) && (*gEmergencyMemory != nil) );
  205. } /* IsEmergencyMemAvail */
  206.  
  207.  
  208. /* IM-Memory: 1-48 */
  209. #if !(kCompileWithQuitOnLowMemory)
  210. OSErr
  211. RecoverEmergencyMemory ( void )
  212. {
  213.     ReallocateHandle ( gEmergencyMemory, kMemCushionSize );
  214.     
  215.     return MemError ();
  216. } /* RecoverEmergencyMemory */
  217. #endif
  218.  
  219. /***  EOF  ***/